﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SharedFleetAccess;

namespace PL___Windows_Inteface
{
    public partial class SharedFleet : Form
    {
        public SharedFleet()
        {
            InitializeComponent();
            cars_header.Text = "Select one Car:";
            car_list.Columns.Add("ImageColum", 0); //Not Show
            car_list.Columns.Add("Reg Number", -2, HorizontalAlignment.Left);
            car_list.Columns.Add("Fuel", -2, HorizontalAlignment.Left);
            car_list.Columns.Add("Price", -2, HorizontalAlignment.Left);
            car_list.Columns.Add("Color", -2, HorizontalAlignment.Left);
            car_list.Columns.Add("State", -2, HorizontalAlignment.Left);
            car_list.Columns.Add("Type", -2, HorizontalAlignment.Left);
            car_list.Columns.Add("Num of Doors", -2, HorizontalAlignment.Left);
            fillCars();

            trucks_header.Text = "Select a Truck to lease:";
            truck_list.Columns.Add("ImageColum", 0); //Not Show
            truck_list.Columns.Add("Reg Number", -2, HorizontalAlignment.Left);
            truck_list.Columns.Add("Fuel", -2, HorizontalAlignment.Left);
            truck_list.Columns.Add("Price", -2, HorizontalAlignment.Left);
            truck_list.Columns.Add("Color", -2, HorizontalAlignment.Left);
            truck_list.Columns.Add("State", -2, HorizontalAlignment.Left);
            truck_list.Columns.Add("Tonnage", -2, HorizontalAlignment.Left);
            truck_list.Columns.Add("Num of Wheels", -2, HorizontalAlignment.Left);
            truck_list.Columns.Add("High", -2, HorizontalAlignment.Left);
            truck_list.Columns.Add("Width", -2, HorizontalAlignment.Left);
            truck_list.Columns.Add("Large", -2, HorizontalAlignment.Left);
            fillTrucks();
             
        }

        
        private void fillCars() {
            String aux;
            FleetAccess.RunFleetAccess();
            List<String> car_ids =  FleetAccess.getCarList();
            foreach (String car in car_ids) {
                Dictionary<String, String> c = FleetAccess.getAttributes(car);
                c.TryGetValue("Reg_Number", out aux);
                ListViewItem item = new ListViewItem(aux); 
                item.SubItems.Add(aux);
                c.TryGetValue("Price", out aux);
                item.SubItems.Add(aux);
                c.TryGetValue("Colour", out aux);
                item.SubItems.Add(aux);
                c.TryGetValue("State", out aux);
                item.SubItems.Add(aux);
                c.TryGetValue("Fuel", out aux);
                item.SubItems.Add(aux);
                 c.TryGetValue("Type_car", out aux);
                item.SubItems.Add(aux);
                 c.TryGetValue("Doors_number", out aux);
                item.SubItems.Add(aux);             
                car_list.Items.Add(item);
            
            }        
        }

        private void fillTrucks()
        {
            String aux;
            FleetAccess.RunFleetAccess();
            List<String> trucks_ids = FleetAccess.getTruckList();
            foreach (String truck in trucks_ids)
            {
                Dictionary<String, String> dic = FleetAccess.getAttributes(truck);
                dic.TryGetValue("Reg_Number", out aux);
                ListViewItem item = new ListViewItem(aux);
                item.SubItems.Add(aux);
                dic.TryGetValue("Price", out aux);
                item.SubItems.Add(aux);
                dic.TryGetValue("Colour", out aux);
                item.SubItems.Add(aux);
                dic.TryGetValue("State", out aux);
                item.SubItems.Add(aux);
                dic.TryGetValue("Fuel", out aux);
                item.SubItems.Add(aux);

                dic.TryGetValue("Tonnage", out aux);
                item.SubItems.Add(aux);
                dic.TryGetValue("Num_Wheels", out aux);
                item.SubItems.Add(aux);
                  dic.TryGetValue("High", out aux);
                item.SubItems.Add(aux);
                dic.TryGetValue("Width", out aux);
                item.SubItems.Add(aux);
                  dic.TryGetValue("Large", out aux);
                item.SubItems.Add(aux);
              
                truck_list.Items.Add(item);


               
                }
            }


        private void reserve_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection aux = car_list.SelectedItems;
            ListView.SelectedListViewItemCollection aux2 = truck_list.SelectedItems;
            if (aux.Count >0)
            {
                String car_id = aux[0].Text;
                DialogResult result = MessageBox.Show("Are you sure to reserve the car with id:" + car_id, "Confirm the reserve", MessageBoxButtons.OKCancel);
                if(result == System.Windows.Forms.DialogResult.OK){
                    FleetAccess.reserveVehicle(car_id);
                    MessageBox.Show("The car was reserved", "Confirmation reserve", MessageBoxButtons.OK);
                
                }
            }else
            if (aux2.Count > 0)
            {
                String truck_id = aux2[0].Text;
                DialogResult result = MessageBox.Show("Are you sure to reserve the truck with id:" + truck_id, "Confirm the reserve", MessageBoxButtons.OKCancel);
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    FleetAccess.reserveVehicle(truck_id);
                    MessageBox.Show("The truck was reserved", "Confirmation reserve", MessageBoxButtons.OK);
                }

            }
            else
            {
                error_lb.Text = "Please select one car or one truck";
            }
        }

        private void car_list_Click(object sender, EventArgs e)
        {
            truck_list.SelectedItems.Clear();
        }

        private void truck_list_Click(object sender, EventArgs e)
        {
            car_list.SelectedItems.Clear();
        }
        }
    }

